package com.all.play.myadapterdrag.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by 聪明一只哈 on 2018/5/17.
 * Time: 2018/5/17  9:54
 */

public class MyItemDecoration extends RecyclerView.ItemDecoration {

    //初始化时设置为竖向
    private int mOrientation=LinearLayoutManager.VERTICAL;

    private Drawable mDivider;


    private int[] attrs=new int[]{
            android.R.attr.listDivider
    };

    public MyItemDecoration(Context context, int mOrientation) {

        TypedArray typedArray = context.obtainStyledAttributes(attrs);
        mDivider = typedArray.getDrawable(0);
        typedArray.recycle();
        setOrietation(mOrientation);

    }

    //设置方向-->分割线是横向还是竖向
    public void setOrietation(int orientation){
        if(orientation!= LinearLayoutManager.HORIZONTAL&&orientation!=LinearLayoutManager.VERTICAL){
            throw new IllegalArgumentException("分割线方向不确定!");
        }
        this.mOrientation=orientation;
    }

    //重写后,RecycleView会回调该绘制方法,需要自己绘制条目的间隔线
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {


        if(mOrientation==LinearLayoutManager.VERTICAL){
                drawVertical(c,parent);
        }else{
                drawHorizontal(c,parent);
        }
        super.onDraw(c, parent, state);
    }

    private void drawHorizontal(Canvas c, RecyclerView parent) {
        int top=parent.getPaddingTop();
        int bottom=parent.getHeight()-parent.getPaddingBottom();

        //所有item的,分别的宽高
        int childCount = parent.getChildCount();
        for (int i=0;i<childCount;i++){
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int left=child.getRight()+params.rightMargin+ Math.round(child.getTranslationX()) ;
            int right=left+mDivider.getIntrinsicWidth();
            mDivider.setBounds(left,top,right,bottom);
            mDivider.draw(c);
        }
    }

    private void drawVertical(Canvas c, RecyclerView parent) {
        //画水平线
        //有外边距
        int left=parent.getPaddingLeft();
        int right=parent.getWidth()-parent.getPaddingRight();

        //所有item的,分别的宽高
        int childCount = parent.getChildCount();
        for (int i=0;i<childCount;i++){
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int top=child.getBottom()+params.bottomMargin+ Math.round(child.getTranslationY()) ;
            int bottom=top+mDivider.getIntrinsicHeight();
            mDivider.setBounds(left,top,right,bottom);
            mDivider.draw(c);
        }


    }

    //获取条目偏移量,所有条目都会调用一次该方法,然后才会调用上方的
    //onDraw()方法绘制分割线
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        //1.首先先获取条目之间的间隔宽度----Rect矩形区域
        //super.getItemOffsets(outRect, view, parent, state);

        if(mOrientation==LinearLayoutManager.VERTICAL){
            outRect.set(0,0,0,mDivider.getIntrinsicHeight());
        }else{
            outRect.set(0,0,mDivider.getIntrinsicHeight(),0);
        }


    }
}
